<template>
  <div class="container">
    <h2 class="title">添加库存项</h2>
    <form @submit.prevent="handleSubmit" class="form">
      <div class="form-group">
        <label for="name" class="label">名称:</label>
        <input
          type="text"
          id="name"
          v-model="item.name"
          required
          class="input"
        />
      </div>
      <div class="form-group">
        <label for="quantity" class="label">数量:</label>
        <input
          type="number"
          id="quantity"
          v-model="item.quantity"
          required
          class="input"
        />
      </div>
      <button type="submit" class="button">提交</button>
    </form>
  </div>
</template>

<script>
import { createInventoryItem } from '@/api/inventory';
import { ref } from 'vue';
import { useRouter } from 'vue-router';

export default {
  setup() {
    const router = useRouter();
    const item = ref({ name: '', quantity: null });

    const handleSubmit = async () => {
      try {
        await createInventoryItem(item.value);
        router.push('/inventory'); // 提交后返回库存列表页面
      } catch (error) {
        console.error('Error creating inventory item:', error);
      }
    };

    return {
      item,
      handleSubmit,
    };
  },
};
</script>

<style scoped>
.container {
  max-width: 400px;
  margin: 2rem auto;
  padding: 2rem;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.title {
  color: #333;
  margin-bottom: 1.5rem;
  text-align: center;
}

.form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  text-align: left;
}

.label {
  font-weight: 500;
  color: #555;
}

.input {
  width: 100%;
  padding: 0.8rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
  transition: border-color 0.3s ease;
}

.input:focus {
  border-color: #007bff;
  outline: none;
  box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
}

.button {
  width: 100%;
  padding: 0.8rem;
  background: #007bff;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.button:hover {
  background: #0056b3;
}

@media (max-width: 480px) {
  .container {
    margin: 1rem;
    padding: 1.5rem;
  }
}
</style>